package Action;

import java.io.IOException;
import java.sql.Date;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Dao.coursedao;
import Dao.noticedao;
import Dao.rankdao;
import Dao.scheduledao;
import Dao.selectiondao;
import Entity.Course;
import Entity.Notice;
import Entity.Room_ced;
import Entity.User;
import mapper.Coursemapper;
import mapper.Noticemapper;
import mapper.Rankmapper;
import mapper.Schedulemapper;
import mapper.Selectionmapper;

/**
 * Servlet implementation class CreatServlet
 */
@WebServlet("/CreatServlet")
public class CreatServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreatServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 HttpSession session = request.getSession();
	        User user =(User) session.getAttribute("user");
	        String id = user.getUid();
        String Course_Name = request.getParameter("Course_Name");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       
        //获取course_Id和schedule_Id并更新
        Rankmapper rm = new rankdao();
		int num = rm.getNum("course_Id");
		int num1 = rm.getNum("schedule_Id");
		String s1=String.valueOf(num);
		String s2=String.valueOf(num1);
		rm.UpdateNum("course_Id");
		rm.UpdateNum("schedule_Id");
		//创建course
		Course c =new Course(); 
		c.setCourse_Id(s1);
        c.setId(id);
        c.setCourse_Name(Course_Name);
       
        //创建schedule
        Room_ced r=new Room_ced();
        r.setSchedule_Id(s2);
        
        String d =request.getParameter("date");
        java.util.Date da=null;
		try {
			da = sdf.parse(d);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.sql.Date date = new java.sql.Date(da.getTime());
		r.setDate(date);
        int order = Integer.parseInt(request.getParameter("order"));
        r.setOrder(order);
        
        //调用数据库函数
        Schedulemapper sm=new scheduledao();
        Coursemapper cm = new coursedao();
        sm.CreatSchedule(r);
        cm.CreatCourse(c);
        String cid=String.valueOf(num);
        String oid = "System";
        Selectionmapper sim = new selectiondao();
		List<String> ls=sim.courseRelatedStudents(cid);
		Schedulemapper slm = new scheduledao();
		Room_ced rc = slm.getSchedule(cid);
		String context ="您已被安排进行一节新的实验课程！课程信息如下：\n课程标题："+rc.getCourse_Id()+"\n时间："+rc.getDate()+"\n（日期）第"+rc.getOrder()+"大节地点："+rc.getRoom_Id()+"\n机房详细信息请进入个人中心->我的课程查看！";
		for(int i=0;i<ls.size();i++){
			Notice n =new Notice();
	        n.setContent(context);
	        n.setFrom_Id(oid);
			String user_id=ls.get(i);
			n.setTo_Id(user_id);
			Noticemapper nm = new noticedao();
		    boolean a =nm.sendMessage(n);}
        request.getRequestDispatcher("class_total.jsp").forward(request,response);
		
	}

}
